package com.cci.kangdao.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cci.kangdao.dao.ExternalAuthMapper;
import com.cci.kangdao.dao.model.ExternalAuthInfo;
import com.cci.kangdao.dto.RoadTestHeader;
import com.gexin.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cci.kangdao.service.impl.ServiceStationServImpl;
import com.cci.kangdao.utilTool.ParameterTool;

@Controller
public class ServiceStationRest {

	private static final Logger log = Logger.getLogger(ServiceStationRest.class);
	
	@Autowired
	private ServiceStationServImpl ssService;
	@Resource
	private ExternalAuthMapper externalAuthMapper;
	
	/**
	 * 获取基础数据接口
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getBaseInfo.aspx")
	public ModelAndView getBaseInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return ssService.getBaseInfo(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 获取企业关联发动机
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getEngsByCom.aspx")
	public ModelAndView getEngsByCom(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return ssService.getEngsByCom(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 根据条件获取服务站
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getStations.aspx")
	public ModelAndView getStations(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return ssService.getStations(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	
	@RequestMapping("/getServiceStationV2.aspx")
	public ModelAndView getServiceStationV2(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return ssService.getServiceStationV2(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}


	@RequestMapping("/getOutServiceStation.aspx")
	public ModelAndView getOutServiceStation(HttpServletRequest request,HttpServletResponse response,@RequestHeader Map<String, String> headerMap){
		try{
			//验证token
			RoadTestHeader header = JSON.parseObject(JSON.toJSONString(headerMap), RoadTestHeader.class);
			log.info(  "header信息:"+header);
			if(null == header){
				String msg = "{\"status\": -99,\"msg\": \"缺少token参数\"}";
				ParameterTool.writeResponse(response, msg);
				return null;
			}
			if(StringUtils.isBlank(header.getSource()) || StringUtils.isBlank(header.getVersion()) || StringUtils.isBlank(header.getToken()) || StringUtils.isBlank(header.getSystemTime())){
				String msg = "{\"status\": -99,\"msg\": \"缺少token参数\"}";
				ParameterTool.writeResponse(response, msg);
				return null;
			}
			ExternalAuthInfo authInfo = externalAuthMapper.getAuthBySource(header.getSource(),header.getVersion());
			if(null == authInfo){
				String msg = "{\"status\": -99,\"msg\": \"未查询到token信息\"}";
				ParameterTool.writeResponse(response, msg);
				return null;
			}
			if(!authInfo.getToken().equals(header.getToken())){
				String msg = "{\"status\": -99,\"msg\": \"token验证失败\"}";
				ParameterTool.writeResponse(response, msg);
				return null;
			}
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return ssService.getOutServiceStation(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}
	
	
	@RequestMapping("/getMetadataForServiceStation.aspx")
	public ModelAndView getMetadataForServiceStation(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return ssService.getMetadataForServiceStation(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
}
